package com.so206.controller.payment;

import com.github.pagehelper.PageInfo;
import com.so206.po.ProxyList;
import com.so206.po.ProxyRecord;
import com.so206.po.SystemUser;
import com.so206.po.ext.ProxySelectExt;
import com.so206.po.ext.SysUserExt;
import com.so206.service.ProxyService;
import com.so206.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 代理相关
 */
@Controller
@RequestMapping("/admin/proxy")
public class ProxyController {

    @Autowired
    private ProxyService proxyService;

    @Autowired
    private UserService userService;

    /**
     * 代理列表
     */
    @RequiresPermissions("proxy_controller")
    @RequestMapping("/proxy_list")
    public String list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer rows,
                       @RequestParam(defaultValue = "false") Boolean ajax,
                       Model model, Integer status, Long uid, String user) {
        PageInfo<ProxySelectExt> bean = proxyService.find_by_page(page, rows, uid, status, user);
        model.addAttribute("bean", bean);
        model.addAttribute("uid", uid);
        model.addAttribute("status", status);
        model.addAttribute("user", user);
        if (ajax) {
            return "admin/proxy/list::pageNode";
        } else {
            return "admin/proxy/list";
        }
    }

    /**
     * 删除代理
     */
    @RequiresPermissions("proxy_controller")
    @RequestMapping("/proxy_delete")
    @ResponseBody
    public Map proxy_delete(Long id) {
        HashMap<Object, Object> map = new HashMap<>();
        ProxyList proxy = proxyService.find_proxy_by_id(id);
        SystemUser user = userService.findUserById(proxy.getUid());
        if (user.getId() != 1) {
            user.setRole(2L);
            userService.updateUser(user);
        }
        proxyService.delete_proxy(id);
        map.put("msg", "删除成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 代理查看
     */
    @RequiresPermissions("proxy_controller")
    @RequestMapping("/proxy_view")
    public String proxy_view(Long id, Model model) {
        ProxyList bean = proxyService.find_proxy_by_id(id);
        model.addAttribute("model", bean);
        if (bean.getUid() != null) {
            SystemUser user = userService.findUserById(bean.getUid());
            model.addAttribute("user", user);
        }
        return "admin/proxy/view";
    }

    /**
     * 代理编辑
     */
    @RequiresPermissions("proxy_controller")
    @RequestMapping("/proxy_edit")
    public String proxy_edit(Long id, Model model) {
        ProxyList bean = proxyService.find_proxy_by_id(id);
        model.addAttribute("model", bean);
        if (bean.getUid() != null) {
            SystemUser user = userService.findUserById(bean.getUid());
            model.addAttribute("user", user);
        }
        return "admin/proxy/edit";
    }

    /**
     * 代理更新
     */
    @RequiresPermissions("proxy_controller")
    @RequestMapping("/proxy_update")
    @ResponseBody
    public Map proxy_update(ProxyList proxyList) {
        HashMap<Object, Object> map = new HashMap<>();
        proxyService.update_proxy(proxyList);
        if (proxyList.getStatus() == 1) {
            SystemUser user = userService.findUserById(proxyList.getUid());
            if (user.getId() != 1) {
                user.setRole(4L);
                userService.updateUser(user);
            }
        } else {
            SystemUser user = userService.findUserById(proxyList.getUid());
            if (user.getId() != 1) {
                user.setRole(2L);
                userService.updateUser(user);
            }
        }
        map.put("msg", "更新成功!");
        map.put("code", 0);
        return map;
    }


    /**
     * 代理记录列表
     */
    @RequiresPermissions("record_list")
    @RequestMapping("/record_list")
    public String record_list(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "20") Integer rows,
                              @RequestParam(defaultValue = "false") Boolean ajax,
                              Model model, Long sub_uid, Long uid, Long order_id) {
        PageInfo<ProxyRecord> bean = proxyService.find_record_by_page(page, rows, uid, sub_uid, order_id);
        model.addAttribute("bean", bean);
        model.addAttribute("uid", uid);
        model.addAttribute("sub_uid", sub_uid);
        model.addAttribute("order_id", order_id);
        if (ajax) {
            return "admin/proxy/record/list::pageNode";
        } else {
            return "admin/proxy/record/list";
        }
    }

    /**
     * 删除记录
     */
    @RequiresPermissions("record_list")
    @RequestMapping("/record_delete")
    @ResponseBody
    public Map record_delete(Long id) {
        HashMap<Object, Object> map = new HashMap<>();
        proxyService.delete_record(id);
        map.put("msg", "删除成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 查看记录
     */
    @RequiresPermissions("record_list")
    @RequestMapping("/record_view")
    public String record_view(Long id, Model model) {
        ProxyRecord record = proxyService.find_record_by_id(id);
        model.addAttribute("model", record);
        return "admin/proxy/record/view";
    }

    /**
     * 用户列表
     */
    @RequiresPermissions("sub_user_list")
    @RequestMapping("/user_list")
    public String index2(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "20") Integer rows,
                         @RequestParam(defaultValue = "false") Boolean ajax,
                         Model model, Long pid, String email, Integer role, String user, Integer parent_uid, HttpSession session) throws Exception {
        SystemUser info = (SystemUser) session.getAttribute("userInfo");
        if (info == null) {
            throw new Exception("获取用户信息失败,请重新登录后重试!");
        }
        PageInfo<SysUserExt> byPage = userService.findByPage(page, rows, user, pid, null, email, info.getId());
        model.addAttribute("bean", byPage);
        model.addAttribute("s", user);
        model.addAttribute("pid", pid);
        model.addAttribute("role", role);
        model.addAttribute("email", email);
        model.addAttribute("parent_uid", parent_uid);
        if (ajax) {
            return "admin/user/list3::pageNode";
        } else {
            return "admin/user/list3";
        }
    }

    /**
     * 查看用户资料
     */
    @RequiresPermissions("sub_user_list")
    @RequestMapping("/view_user")
    public String view(Model model, Long id) {
        SystemUser user = userService.findUserById(id);
        model.addAttribute("model", user);
        return "admin/user/view2";
    }

    /**
     * 代理记录列表
     */
    @RequiresPermissions("my_proxy_list")
    @RequestMapping("/user_proxy")
    public String user_proxy(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "20") Integer rows,
                             @RequestParam(defaultValue = "false") Boolean ajax,
                             Model model, Long sub_uid, Long order_id, HttpSession session) throws Exception {
        SystemUser info = (SystemUser) session.getAttribute("userInfo");
        if (info == null) {
            throw new Exception("获取用户信息失败,请重新登录后重试!");
        }
        PageInfo<ProxyRecord> bean = proxyService.find_record_by_page(page, rows, info.getId(), sub_uid, order_id);
        model.addAttribute("bean", bean);
        model.addAttribute("sub_uid", sub_uid);
        model.addAttribute("order_id", order_id);
        if (ajax) {
            return "admin/proxy/record/list2::pageNode";
        } else {
            return "admin/proxy/record/list2";
        }
    }

    /**
     * 查看记录
     */
    @RequiresPermissions("record_list")
    @RequestMapping("/record_view2")
    public String record_view2(Long id, Model model) {
        ProxyRecord record = proxyService.find_record_by_id(id);
        model.addAttribute("model", record);
        return "admin/proxy/record/view";
    }

}
